﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
	/// <summary>
	/// 后裔转种
	/// </summary>
	public class DescendantTransferRepository:IDescendantTransferRepository
	{
		/// <summary>
		/// 上下文
		/// </summary>
		private readonly AgricultureDbContext _dbcontext;
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="dbcontext"></param>
		public DescendantTransferRepository(AgricultureDbContext dbcontext)
		{
			_dbcontext = dbcontext;
		}
		/// <summary>
		/// 获取后裔转种列表
		/// </summary>
		/// <returns></returns>
		public async Task<IQueryable<Descendanttransfer>> GetDescendanttransfers()
		{
			var descendanttransfers = _dbcontext.Descendanttransfers.Where(c=>c.IsDel==false).AsQueryable();
			return descendanttransfers;
		}
		/// <summary>
		/// 删除
		/// </summary>
		/// <param name="loginId"></param>
		/// <param name="idList"></param>
		/// <returns></returns>
		public async Task<int> DelDescendanttransfer(string loginId,params string[] eartag)
		{
			if (eartag.Length <= 0)
			{
				return 0;
			}
			var list=_dbcontext.Descendanttransfers.Where(x => eartag.Contains(x.EarTag));
			foreach(var item in list)
			{
				item.IsDel=true;
				item.UpdateName = loginId;
				item.UpdateTime = DateTime.Now;
			};
			//更改断奶删除字段
			var lamb=_dbcontext.Lambweaninginfos.Where(x => eartag.Contains(x.EarTag));
			foreach(var item in lamb)
			{
				item.IsDel = false;
			}
			//删除后更改档案信息
			var sheeps = _dbcontext.Sheepfilemodels.Where(c => eartag.Contains(c.EarTag)).ToList();
			foreach (var item in sheeps)
			{
				var lambInfo = _dbcontext.Descendanttransfers.FirstOrDefault(l => l.EarTag == item.EarTag);
				item.Growthstage = 2;
				item.SheepType = 3;
				item.ColumnId = lambInfo.CurrentField;
				item.BuildingId = lambInfo.CurrentBuilding;
			}
			return await _dbcontext.SaveChangesAsync();
		}
		/// <summary>
		/// 添加后裔转种
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<int> AddDelDescendanttransfer(List<Descendanttransfer> entity)
		{
			_dbcontext.Descendanttransfers.AddRangeAsync(entity);
			return await _dbcontext.SaveChangesAsync();
		}
		/// <summary>
		/// 反填后裔转种
		/// </summary>
		/// <param name="eartag"></param>
		/// <returns></returns>
		public async Task<Descendanttransfer> DetaileDescendanttransfer(string eartag)
		{
			var descendanttransfer = _dbcontext.Descendanttransfers.FirstOrDefault(c => c.EarTag == eartag);
			return descendanttransfer;
		}
		/// <summary>
		/// 修改后裔转种
		/// </summary>
		/// <param name="entity"></param>
		/// <returns></returns>
		public async Task<int> UpdateDescendanttransfer(Descendanttransfer entity)
		{
			_dbcontext.Entry(entity).State = EntityState.Modified;
			return await _dbcontext.SaveChangesAsync();
		}
	}
}
